package com.gxa.logistics.controller;

import com.gxa.logistics.common.base.PageWrapperDTO;
import com.gxa.logistics.common.base.RespResult;
import com.gxa.logistics.domain.dto.BillDTO;
import com.gxa.logistics.domain.dto.PlanRecordDTO;
import com.gxa.logistics.domain.dto.YearBillDTO;
import com.gxa.logistics.domain.vo.BillVO;
import com.gxa.logistics.domain.vo.PlanRecordVO;
import com.gxa.logistics.domain.vo.YearBillRecordVO;
import com.gxa.logistics.service.BillService;
import com.gxa.logistics.service.PlanRecordService;
import com.gxa.logistics.service.YearBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "财务管理模块")
@RestController
@RequestMapping("/Finance")
public class FinanceController {

    @Autowired
    private BillService billService;
    @Autowired
    private PlanRecordService planRecordService;
    @Autowired
    private YearBillService yearBillService;

    @ApiOperation("出入库台账")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "当前页码数",defaultValue = "1",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示条数",defaultValue = "5",paramType = "query",dataType = "String")
    })
    @PostMapping("/SelectBill")
    public RespResult<PageWrapperDTO<BillVO>> selectBIll(@RequestBody BillDTO billDTO){
        return billService.selectBill(billDTO);
    }

    @ApiOperation("计划清算")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "当前页码数",defaultValue = "1",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示条数",defaultValue = "5",paramType = "query",dataType = "String")
    })
    @PostMapping("/SelectPlanRecord")
    public RespResult<PageWrapperDTO<PlanRecordVO>> selectPlanRecord(@RequestBody PlanRecordDTO planRecordDTO){
        return planRecordService.selectPlanRecord(planRecordDTO);
    }

    @ApiOperation("年度台账")
    @PostMapping("/SelectYearBill")
    public RespResult<List<YearBillRecordVO>> selectYearBill(@RequestBody YearBillDTO yearBillDTO){
        List<YearBillRecordVO> yearBillRecordVOS=new ArrayList<>();
        List<YearBillRecordVO> yearBillRecordVOList =  yearBillService.selectAllRecord(yearBillDTO);//所有记录头
        for (int i = 0; i < yearBillRecordVOList.size(); i++) {
            yearBillRecordVOS.add(yearBillRecordVOList.get(i));
            for (int j = 1; j <=12; j++) {
                YearBillRecordVO yearBillRecordVO = new YearBillRecordVO();
                List<YearBillRecordVO> inNumList = yearBillService.selectInNum(yearBillRecordVOS.get(i).getMaterialId(),j);
                List<YearBillRecordVO> outNumList = yearBillService.selectOutNum(yearBillRecordVOS.get(i).getMaterialId(),j);
                List<YearBillRecordVO> remainNum = yearBillService.selectRemainNum(yearBillRecordVOS.get(i).getMaterialId(),j);
                Double inNum = 0.00;
                Double outNum = 0.00;
                Double reNum = 0.00;
                if( remainNum.size()!=0){
                    reNum= remainNum.get(0).getRemainNum();
                }
                if(inNumList.size()!=0) {
                    for (int k = 0; k <inNumList .size(); k++){
                        inNum += inNumList.get(i).getInNUm();
                    }
                }
                if(inNumList.size()!=0) {
                    for (int k = 0; k < outNumList.size(); k++) {
                        outNum += outNumList.get(i).getOutNUm();
                    }
                }
                yearBillRecordVO.setInNUm(inNum);
                yearBillRecordVO.setOutNUm(outNum);
                yearBillRecordVO.setRemainNum(reNum);
                yearBillRecordVOS.add(yearBillRecordVO);

            }
        }
        return RespResult.success(yearBillRecordVOS);
    }

    @ApiOperation("查询全部操作类型")
    @GetMapping("/SelectBillAllOpType")
    public RespResult selectBIll(){
        List<String> billDTOS =  billService.selectAllOpType();
        return RespResult.success(billDTOS);
    }

    @ApiOperation("查询账单表全部计划类型")
    @GetMapping("/SelectBillAllPlanType")
    public RespResult selectBillAllPlanType(){
        List<String> list =  planRecordService.selectAllPlanType();
        return RespResult.success(list);
    }

    @ApiOperation("查询操作表的所有计划类型")
    @GetMapping("/selectAllOpPlanType")
    public RespResult selectAllOpPlanType(){
        List<String> list =  yearBillService.selectAllOpPlanType();
        return RespResult.success(list);
    }

    @ApiOperation("更新计划表的项目结算状态")
    @GetMapping("/updatePlanById")
    public RespResult updatePlanById(Integer planId){
        planRecordService.updatePlanById(planId);
        return RespResult.success();
    }

}
